from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time

# 代码的执行时间远远快于页面的加载速度
# 在翻页或者加载新页面的时候，需要进行页面等待
# 等待方式三种
# 1.强制等待    time.sleep  不推荐
# 2.显式等待
# 3.隐式等待


driver = webdriver.Edge()

url = "https://www.baidu.com/"

driver.get(url)

driver.find_element(By.ID,"kw").send_keys("美女")
driver.find_element(By.ID,"su").click()

# 强制等待
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,"[srcid='4']").click()

# 显式等待
# 等待某个元素加载完成    每隔0.5秒检查一次  最多等待5秒时间
# WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"[srcid='4']")))
# driver.find_element(By.CSS_SELECTOR,"[srcid='4']").click()

# 隐式等待
driver.implicitly_wait(5)
driver.find_element(By.CSS_SELECTOR,"[srcid='4']").click()


time.sleep(5)
driver.quit()